package com.heima.search.listeners;

import com.alibaba.fastjson.JSON;
import com.heima.common.constant.ArticleConstant;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.search.service.ESService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ESListener {
    @Autowired
    ESService esService;
    @KafkaListener(topics = ArticleConstant.ARTICLE_ES_SYNC_TOPIC)
    public void articleAddToESListener(String msg)
    {
        if (StringUtils.isNotBlank(msg))
        {
            log.info("收到一条来自 {} 主题 的信息:{}",ArticleConstant.ARTICLE_ES_SYNC_TOPIC,msg);
            SearchArticleVo searchArticleVo = JSON.parseObject(msg, SearchArticleVo.class);
            esService.saveSearchArticleVo(searchArticleVo);
        }
    }
}
